package handler

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"yk1-assistant/models"
)

func ListSmall(c *gin.Context) {
	var list []models.SmallCarList
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "2"))
	offset := (page - 1) * pageSize
	models.DB.Table("small_cars").Select(
		"small_cars.id AS CarId",
		"small_cars.car_code AS CarCode",
		"small_cars.car_name AS CarName",
		"small_cars.car_num AS CarNum",
		"small_types.type_name AS TypeName",
		"small_types.type_talk AS TypeTalk",
	).Joins("LEFT JOIN small_types ON small_types.id = small_cars.tid").
		Offset(offset).Limit(pageSize).Find(&list)
	var total int64
	models.DB.Model(&models.Order{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  list,
		"total": total,
	})
	return
}
